package unimelb.filesync.common.protocol;

import java.io.Console;
import java.util.Scanner;

import unimelb.filesync.common.crypto.CryptoUtils;
import unimelb.filesync.common.misc.ProtocolContext;

public class ReadPasswordInput implements IFileSyncPhase {

    private final ProtocolContext context;
    
    private final CryptoUtils cryptoUtils;

    public ReadPasswordInput(ProtocolContext context) {
        super();
        this.context = context;
        this.cryptoUtils = new CryptoUtils();
    }

    @Override
    public void execute() {
        String password = null;
        
        final Console console = System.console();
        if (console != null) {
            console.printf("Enter password:");
            password = new String(console.readPassword());
        } else {
            System.out.println("System.console is not supported. Fall back to System.in");
            System.out.print("Enter password:");
            password = new Scanner(System.in).nextLine();
        }

        context.addObject(ProtocolContext.PASSWORD, cryptoUtils.sha1Hash(password));
    }
}
